#ifndef NETWORKTRANSFER_H
#define NETWORKTRANSFER_H
#include "proto/Exchange.pb.h"

#include "deviceinfo.h"
#include <QObject>
#include <QTcpSocket>
#include <QTimer>

class NetWorkHandler : public QObject
{
    Q_OBJECT
public:
    explicit NetWorkHandler(QObject *parent = nullptr);
    void initInfo(DeviceInfo *ntInfo,int type) ;
    enum TransferType
    {
        PASSIVE,
        ACTIVE
    };
    //数据处理
    //大端小段
    int bytesToInt(QByteArray b) ;
    QByteArray intToBytes(int n) ;
    //处理粘包
    QByteArray m_buffer ;
    //处理信息
    void deal(QByteArray buffer);
    //处理协议数据
    virtual void dealProto(int i,BigPack::Exchange resv_exc);
    //压缩
    QByteArray GzipCompress(QByteArray postBody);
    QByteArray GZipUnCompress(QByteArray src);
    //序列化发送
    void serializeSend(BigPack::Exchange ex);
    //发送设备信息
    void sendHostInfo(DeviceInfo *info,int type);
    //socket
    QTcpSocket *m_tcpSocket ;
    //设备信息
    DeviceInfo *m_ntInfo ;
    //resourceId
    QString m_transferId ;
    //transfer类型
    int m_type;
    //重连定时器
    QTimer *m_timerReconnect ;
signals:
    void finished();
    void connectedStatus(bool);
public slots:
    void createSocket();
    void removeSocket();
protected:
    virtual void createToHandler();
private slots:
    //已经连接成功，准备数据传输
    void handleConnected();
    //显示错误SOCKET
    void handleSocketError(QAbstractSocket::SocketError);
    //接受信息
    void readMessage();
    //定时重连接
    void timerReconnectTick() ;
    //连接状态改变
    void socketStateChanged(QAbstractSocket::SocketState state);

};

#endif // NETWORKTRANSFER_H
